package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.utils.Tools;

public class DWDialogMenu extends DWControl
{
	private DWFrame m_frame = null;
	private DWTitle m_title = null;
	private DWListbox m_listbox = null;
	private DWTextbox m_textbox = null;
	private DWBackground m_background = null;
	private DWImageBox m_exit = null;
	private final int W_SHOW;//显示宽度
	private int m_menus_index = 0;//功能菜单下标
	
	public DWDialogMenu(String header, String dialog, String[] menus) {
		this(header, dialog, menus, 0, 0);
	}
	
	public DWDialogMenu(String header, String dialog, String[] menus, int x, int y) {
		setPoint(x, y);
		m_frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER);
		W_SHOW = m_frame.m_show_w - 40;
		setHeader(header);
		setDialog(dialog, menus);
		setMenus(menus);
	}
	
	public void setHeader(String header) {
		m_title = new DWTitle(header, m_frame);
//		Bitmap corner = DWControlsManager.getInstance().createControlImage("liaotianxbjk_1");
//		Bitmap side = DWControlsManager.getInstance().createControlImage("liaotianxbjk_2");
		m_background = new DWBackground(W_SHOW + 20, m_frame.getShowHeight() - m_title.getShowHeight() - 10);
		m_background.setNearAnchor(m_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 10, 5);
		Bitmap exit = DWControlsManager.getInstance().createControlImage("fanhuianniu_1");
		m_exit = new DWImageBox(exit);
		m_exit.setNearAnchor(m_title, Tools.RIGHT_TOP, Tools.RIGHT_TOP);
		m_frame.addControl(m_title);
		m_frame.addControl(m_background);
		m_frame.addControl(m_exit);
	}
	
	public void setDialog(String dialog, String[] menus) {
		if (m_textbox != null) {
			if (m_frame != null && m_frame.contains(m_textbox)) {
				m_frame.removeControl(m_textbox);
			}
			m_textbox.destroy();
			m_textbox = null;
		}
		int num = 3;
		if (menus == null || menus.length == 0) {
			num = 1;
		}
		int tempH = (m_frame.getShowHeight() - m_title.getShowHeight() - 35) / num;
		m_textbox = new DWTextbox(dialog == null ? "" : dialog, W_SHOW, tempH);
//		if (m_textbox.getShowHeight() > tempH) {
//			m_textbox.destroy();
//			m_textbox = null;
//			m_textbox = new DWTextbox(dialog, W_SHOW, tempH);
//		}
		m_textbox.setNearAnchor(m_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 10);
		m_frame.addControl(m_textbox);
	}
	
	public void setMenus(String[] menus) {
		if (m_listbox != null) {
			if (m_frame != null && m_frame.contains(m_listbox)) {
				m_frame.removeControl(m_listbox);
			}
			m_listbox.destroy();
			m_listbox = null;
		}
		if (menus != null) {
			int tempH = m_frame.getShowHeight() - m_title.getShowHeight() - 20;
			if (m_textbox == null) {
				m_listbox = new DWListbox(W_SHOW, tempH);
				m_listbox.setNearAnchor(m_title, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 5);
			} else {
				m_listbox = new DWListbox(W_SHOW, tempH - m_textbox.getShowHeight() - 5);
				m_listbox.setNearAnchor(m_textbox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 5);
			}
			for (int i = 0; i < menus.length; i++) {
				DWListSubItem lsi = new DWListSubItem();
				DWTextbox tb = new DWTextbox(menus[i], W_SHOW);
				tb.setNearAnchor(lsi, Tools.LEFT_TOP, Tools.LEFT_TOP);
				lsi.addControls(tb);
				m_listbox.addSubItem(lsi);
			}
			m_listbox.addListener(m_listbox_lis);
			m_frame.addControl(m_listbox);
		}
	}
	
	public void render(DWGraphics g)
	{
		if (!m_is_show) {
			return;
		}
		m_frame.render(g);
	}

	public void logic()
	{
		
	}
	private boolean m_is_destruct = true;
	/**
	 * 设置自动销毁
	 * @param destruct true为自动销毁
	 */
	public void setDestruct(boolean destruct) {
		m_is_destruct = destruct;
	}
	/**
	 * 得到触摸菜单的下标
	 * @return
	 */
	public int getMenusIndex() {
		return m_menus_index;
	}
	
	private DWListener m_listbox_lis = new DWListener() {
		public void OnDoubleClick() {
		}
		public void OnClick() {
			if (m_listener != null) {
				m_menus_index = m_listbox.getTouchIndex();
				m_listener.OnClick();
			}
		}
	};
	

	public boolean doClick(float x, float y)
	{
		if (m_frame.checkPointOnRect(x, y)) {
			if (m_listbox != null) {
				if (m_listbox.doClick(x, y)) {
					return true;
				}
			}
			if (m_exit != null) {
				if (m_exit.doClick(x, y)) {
					destruct();
					return true;
				}
			}
		} else {
			if (m_is_destruct)
				destruct();
		}
		return true;
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (m_frame.checkPointOnRect(start.x, start.y)) {
			if (m_textbox != null) {
				if (m_textbox.doScroll(start, end, distanceX, distanceY)) {
					return true;
				}
			}
			if (m_listbox != null) {
				if (m_listbox.doScroll(start, end, distanceX, distanceY)) {
					return true;
				}
			}
		} else {
			if (m_is_destruct)
				destruct();
		}
		return false;
	}

}
